package cn.linkai.hadoop.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

public class WordCount {

    public static void main(String[] args) throws IOException, IOException {
        Configuration conf=new Configuration();
//        config.addResource("core-site.xml");
//        config.addResource("hdfs-site.xml");
//        config.addResource("mapred-site.xml");
//        config.addResource("yarn-site.xml");
//        config.set("mapreduce.framework.name", "yarn");//集群的方式运行，非本地运行
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        Job job=Job.getInstance(conf);
        job.setJobName("wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));

        boolean success= false;
        try {
            success = job.waitForCompletion(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        if(success){
            System.out.println("success");
        }else{
            System.err.println("error");
        }
    }
}
